package com.snoweagler.JmsSend;

import com.snoweagler.verify.ImageCode;
import com.snoweagler.verify.ImageCodeGenerator;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
@RequestMapping("/imageCode")
public class ImageVerifyController  {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ImageCodeGenerator imageCodeGenerator;
    @RequestMapping("/image")
    public void creatImgCode(HttpServletResponse response, HttpSession session){
        ImageCode imageCode = imageCodeGenerator.generatorImageCode();
        BufferedImage image = imageCode.getImage();
        try {
            ImageIO.write(image,"png",response.getOutputStream());
//            redisTemplate.boundSetOps("imageCode").add(imageCode);
            session.setAttribute("imageCode",imageCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
